package com.yunji.cabinet.device;

import com.yunji.base.cache.BaseCache;
import com.yunji.cabinet.cache.CabinetCache;
import com.yunji.cabinet.domain.DeviceEventDO;
import com.yunji.cabinet.dto.device.DeviceCommandDTO;
import com.yunji.cabinet.dto.device.DeviceEventDTO;
import com.yunji.cabinet.vo.DeviceEventVO;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.model.cache.CabinetCacheDTO;
import com.yunji.common.model.domain.MemberDO;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.utils.JsonUtil;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.coffee.entity.dto.DeviceInfoDTO;
import com.yunji.system.context.SystemContext;


/**
 * @author : peter-zhu
 * @date : 2025/1/21 16:59
 * @description : TODO
 **/
public class DeviceConvert {

    public static DeviceEventDO initDeviceEvent(DeviceEventDTO event) {
        DeviceEventDO domain = new DeviceEventDO();
        domain.setEventDataId(event.getEventDataId());
        domain.setEventDataCode(event.getEventDataCode());
        DeviceCommandDTO commandDTO = event.getCommandDTO();
        domain.setEventType(commandDTO.getEvent());
        domain.setEventCmd(commandDTO.getCmd());
        domain.setEventParam(JsonUtil.toJsonString(commandDTO));
        domain.setImei(commandDTO.getImei());
        CabinetCacheDTO cabinet = CabinetCache.getCabinet(commandDTO.getImei());
        domain.setContainerId(cabinet.getContainerId());
        domain.setContainerName(cabinet.getContainerName());

        domain.setContainerType(cabinet.getTypeCode());
        domain.setBelongId(cabinet.getBelongId());
        domain.setAgentId(cabinet.getAgentId());
        domain.setAgentName(cabinet.getAgentName());
        domain.setSiteId(cabinet.getSiteId());
        domain.setSiteName(cabinet.getSiteName());
        domain.setLocationId(cabinet.getLocationId());
        domain.setLocationName(BaseCache.getSiteLocationName(cabinet.getLocationId()));

        LoginInfo user = SystemContext.getUser();
        if (user != null) {
            domain.setUserType(user.getUserType());
            domain.setPhone(user.getPhone());
            domain.setRealName(user.getRealName());
            domain.setUserId(user.getUserId());
        } else if (event.getMemberId() != null) {
            MemberDO member = BaseCache.getMember(event.getMemberId());
            domain.setUserType(UserType.USER.getCode());
            domain.setPhone(member.getPhone());
            domain.setRealName(member.getName());
            domain.setUserId(member.getId());
        }
        domain.setProductMsg(event.getProductMsg());
        DeviceInfoDTO info = DeviceInfoCache.getInfo(commandDTO.getImei());
        if (info != null)
            domain.setBeforeCmd(JsonUtil.toJsonString(info));
        return domain;
    }

    public static DeviceEventVO domain2VO(DeviceEventDO event) {
        DeviceEventVO deviceEventVO = new DeviceEventVO();
        BeanUtils.clone(event, deviceEventVO);
        deviceEventVO.setEvent(DeviceEventEnum.getDesc(event.getEventType()));
        deviceEventVO.setCommand(DeviceCommandEnum.getDesc(event.getEventCmd()));
        deviceEventVO.setContainerType("通用柜");
        deviceEventVO.setUserType(UserType.getText(event.getUserType()));
        return deviceEventVO;
    }

}
